package aiku.numericalcontroloa.Controller.Die;

import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Model.Dto.DieCustomDto;
import aiku.numericalcontroloa.Service.DieCustomService;
import aiku.numericalcontroloa.Service.DieOrderService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 马文杰
 * @version 1.0
 */
@RestController
@RequestMapping("/dieCustom")
public class DieCustomController {
    @Autowired
    private DieCustomService dieCustomService;
    @Autowired
    private DieOrderService dieOrderService;

    /**
     * 添加客户信息
     */
    @PostMapping("/addCustom")
    public Result addCustom(@RequestBody DieCustomDto dieCustomDto) {
        return dieCustomService.addCustom(dieCustomDto);
    }

    /**
     * 修改客户信息
     */
    @PostMapping("/updateCustom")
    public Result updateCustom(@RequestBody DieCustomDto dieCustomDto) {
        return dieCustomService.updateCustom(dieCustomDto);
    }

    /**
     * 通过昵称模糊查询客户信息
     */
    @RequestMapping("/selectCustomByNickName")
    public Result selectCustomByNickName(@RequestParam("name") String name) {
        return Result.success(dieCustomService.selectCustomByNickName(name));
    }

    /**
     * 通过id删除客户信息
     */
    @RequestMapping("/deleteCustomById")
    public Result deleteCustomById(@RequestParam("id") String id) {
        return dieCustomService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 查询所以客户信息
     */
    @RequestMapping("/selectAllCustom")
    public Result selectAllCustom() {
        return Result.success(dieCustomService.list());
    }

    /**
     * 分页查询用户信息
     */
    @RequestMapping("/selectCustomByPage")
    public Result selectCustomByPage(@RequestParam("page") Long page, @RequestParam("size") Long size) {
        return dieCustomService.selectCustomByPage(page, size);
    }

    /**
     * 通过客户名称来查询该用户的账单信息
     */
    @RequestMapping("/userNameDieOrderInfo")
    public Result userNameDieOrderInfo(@RequestParam("name") String name,
            @RequestParam(value = "startDate", required = false) String stratD,
            @RequestParam(value = "endDate", required = false) String endD) {
        LocalDate stratDate = null;
        LocalDate endDate = null;
        if (stratD != null || endD != null) {
            stratDate = LocalDate.parse(stratD, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            endDate = LocalDate.parse(endD, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        return dieOrderService.userNameDieOrderInfo(name, stratDate, endDate);
    }

    /**
     * 分页查询客户列表
     * 可根据公司名、客户名、联系电话、地址模糊查询
     * 
     * @param dieCustomDto
     * @return
     */
    @PostMapping("/getCustomList")
    public Result getCustomList(@RequestBody DieCustomDto dieCustomDto) {
        return dieCustomService.getCustomList(dieCustomDto);
    }

    @PostMapping("/setReceivables")
    public Result setReceivables(@RequestParam("id") String id, @RequestParam("accountId") String accountId) {
        return dieOrderService.setReceivables(id, accountId);
    }

    @GetMapping("/getCustomById")
    public Result getCustomById(@RequestParam("id") String id) {
    	return Result.success(dieCustomService.getById(id));
    }

}
